<?php

// compile tex file to pdf
function toPdf($texFileName) {
    // need to use absolute path, maybe add $PATH ?
    $pathOfBin = "/usr/local/texlive/2014/bin/x86_64-linux/";
	$t = $texFileName;
	$clean = "rm $t.log $t.aux $t.pdf $t.xdv";
	$command = $pathOfBin . "xelatex -no-pdf $texFileName" . ".tex";
	$command2 = $pathOfBin . "xdvipdfmx $texFileName" . ".xdv";
	
	/* clean .aux .log .xdv and .pdf file */
	exec($clean);
	
	/* tex -> xdv */
	exec($command, $info, $var1);
	
	/* xdv -> pdf */
	exec($command2, $info, $var2);
	
	/**
	 * Success: $var1 == 0 && $var2 == 0
	 * Failed : maybe have not the permision of file associated 
	 */
	if ($var1 + $var2 == 0) {
		echo "### Success. ###<br/>\n";
	} else {
		echo "### Failed. ###<br/>\n";
	}
}

?>
